using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Firestarter.DB.Schema.BO;
using Firestarter.DB_DomainModel.Logic;
using FireStarterModeller.BO;
using Firestarter.DB.Schema.Logic;
using FireStarterModeller.Test;
using FireStarterModeller.UI;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using DockStyle=System.Windows.Forms.DockStyle;
using FormStartPosition=Habanero.Faces.Base.FormStartPosition;
using PictureBoxSizeMode=Habanero.Faces.Base.PictureBoxSizeMode;

namespace Firestarter.DB.Schema.UI
{
    public class DbSchemaReverseEngineeringWizardControl : UserControlWin
    {
        private IWizardControl _wizardControl;
        private DbSchemaReverseEngineerWizardController _reverseEngineerWizardController;
        private IGroupBox _imageGroupBox;
        private ImageStackControl _imageStackControl;

        public DbSchemaReverseEngineeringWizardControl(IFireStarterFormsController fireStarterFormsController, ReverseEngineering reverseEngineering, IDBChangesImporter importer)
        {
            IControlFactory factory = GlobalUIRegistry.ControlFactory;
            _reverseEngineerWizardController = new DbSchemaReverseEngineerWizardController(fireStarterFormsController, reverseEngineering, importer);
            Size = new Size(800, 600);
            Padding = new Padding(5);
            _wizardControl = factory.CreateWizardControl(_reverseEngineerWizardController);
            _wizardControl.MessagePosted += (message => MessageBox.Show(message));
            _wizardControl.StepChanged += WizardControl_OnStepChanged;
            SetupImagePanel(factory);
            BorderLayoutManager layoutManager = factory.CreateBorderLayoutManager(this);
            layoutManager.BorderSize = 10;
            layoutManager.GapSize = 10;
            layoutManager.AddControl(_wizardControl, BorderLayoutManager.Position.Centre);
            layoutManager.AddControl(_imageGroupBox, BorderLayoutManager.Position.West);
            _wizardControl.Start();
        }

        private void SetupImagePanel(IControlFactory factory)
        {
            _imageGroupBox = factory.CreateGroupBox("Overview");
            Bitmap step1Image = Images._1_database_selected;
            _imageGroupBox.Width = step1Image.Width + 5;
            _imageStackControl = new ImageStackControl();
            _imageStackControl.Dock = DockStyle.Fill;
            _imageGroupBox.Controls.Add(_imageStackControl);
            _imageStackControl.AddObjectImages(_reverseEngineerWizardController.Step1, Images._1_database_unselected, Images._1_database_selected);
            _imageStackControl.AddObjectImages(_reverseEngineerWizardController.Step2, Images._2_selecttable_unselected, Images._2_selecttable_selected);
            _imageStackControl.AddObjectImages(_reverseEngineerWizardController.Step3, Images._3_import_unselected, Images._3_import_selected);
            //_imageStackControl.AddObjectImages(_reverseEngineerWizardController.Step4, Images._4_convert_unselected, Images._4_convert_selected);
            _imageStackControl.AddObjectImages(new object(), Images._5_summary_unselected, Images._5_summary_selected);
            
            //IPanel panel = factory.CreatePanel();
            //panel.Dock = DockStyle.Fill;
            //_imageGroupBox.Controls.Add(panel);
            //GridLayoutManager gridLayoutManager = new GridLayoutManager(panel, factory);
            //gridLayoutManager.SetGridSize(5, 1);
            //gridLayoutManager.GapSize = 0;
            //gridLayoutManager.BorderSize = 0;
            //gridLayoutManager.FixAllRowsBasedOnContents();
            //CreateAndAddPictureBox(factory, gridLayoutManager, step1Image);
            //CreateAndAddPictureBox(factory, gridLayoutManager, Images.Step2);
            //CreateAndAddPictureBox(factory, gridLayoutManager, Images.Step3);
            //CreateAndAddPictureBox(factory, gridLayoutManager, Images.Step4);
            //CreateAndAddPictureBox(factory, gridLayoutManager, Images.Step5);
        }

        //private static Bitmap FlipBitmap(Bitmap step1Image)
        //{
        //    Bitmap bitmap = (Bitmap) step1Image.Clone();
        //    bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
        //    return bitmap;
        //}

        //private IPictureBox CreateAndAddPictureBox(IControlFactory factory, GridLayoutManager gridLayoutManager, Bitmap image)
        //{
        //    IPictureBox pictureBox = factory.CreatePictureBox();
        //    pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
        //    pictureBox.Image = image;
        //    gridLayoutManager.AddControl(pictureBox);
        //    return pictureBox;
        //}

        private void WizardControl_OnStepChanged(IWizardStep wizardStep)
        {
            _imageStackControl.SetCurrentObject(wizardStep);
        }

        public static void ShowDBReverseEngineeringWizard(IFireStarterFormsController fireStarterFormsController, ReverseEngineering reverseEngineering, IDBChangesImporter importer)
        {

            IControlFactory factory = GlobalUIRegistry.ControlFactory;
            IFormHabanero form = factory.CreateForm();
            BorderLayoutManager layoutManager = factory.CreateBorderLayoutManager(form);
            DbSchemaReverseEngineeringWizardControl reverseEngineeringWizardControl = new DbSchemaReverseEngineeringWizardControl(fireStarterFormsController, reverseEngineering, importer);
            form.Size = Size.Add(reverseEngineeringWizardControl.Size, new Size(5, 10));
            layoutManager.AddControl(reverseEngineeringWizardControl, BorderLayoutManager.Position.Centre);
            reverseEngineeringWizardControl._wizardControl.Finished += (sender,e) => form.Close();
            reverseEngineeringWizardControl._wizardControl.CancelButton.Click += (sender,e) => form.Close();
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();

        }
    }
}
